package com.jhgsys.internal.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.dto.PostDto;
import com.jhgsys.internal.system.dto.TenantDto;
import com.jhgsys.internal.system.entity.Post;
import com.jhgsys.internal.system.mapper.PostMapper;
import com.jhgsys.internal.system.service.IPostService;
import com.jhgsys.internal.system.service.ITenantService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author wanghw
 * @CreateTime 2024/7/25 17:34
 * @Description 岗位管理持久化
 * @Version 1.0
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class IPostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {

    @Autowired
    private ITenantService tenantService;

    /**
     * 分页插叙岗位
     * @param post    岗位对象（用于传递查询条件）
     * @param request request
     * @return
     */
    @Override
    public IPage<Post> findPosts(Post post, QueryRequest request) {
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Post::getDeletedFlg, 0);

        if(StringUtils.isNotBlank(post.getTenantId())){
            queryWrapper.eq(Post::getTenantId,post.getTenantId());
        }
        if(StringUtils.isNotBlank(post.getPostName())){
            queryWrapper.like(Post::getPostName,post.getPostName());
        }
        if(StringUtils.isNotBlank(post.getPostCode())){
            queryWrapper.eq(Post::getPostCode,post.getPostCode());
        }
        if(StringUtils.isNotBlank(post.getStatus())){
            queryWrapper.eq(Post::getStatus,post.getStatus());
        }
        queryWrapper.orderByAsc(Post::getSort);
        Page<Post> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 按岗位ID查询岗位
     * @param postId 岗位Id
     * @return
     */
    @Override
    public Post findById(String postId) {
        Post post = this.baseMapper.selectOne(new LambdaQueryWrapper<Post>().eq(Post::getPostId,postId).last(" LIMIT 1 "));
        return post;
    }
    /**
     * 按岗位ID查询岗位
     * @param postId 岗位Id
     * @return
     */
    @Override
    public PostDto findDtoById(String postId) {
        PostDto postDto = new PostDto();
        Post post = this.baseMapper.selectOne(new LambdaQueryWrapper<Post>().eq(Post::getPostId,postId).last(" LIMIT 1 "));
        BeanUtils.copyProperties(post,postDto);
        if(null != post){
            TenantDto tenantDto = tenantService.findTenantByTenantId(post.getTenantId());
            postDto.setTenantName(tenantDto.getTenantName());
        }


        return postDto;
    }

    /**
     * 出啊关键岗位
     * @param post 待新增的岗位
     */
    @Override
    @Transactional
    public void createPost(Post post) {
        post.setCreatedTime(new Date());
        this.baseMapper.insert(post);

    }

    /**
     * 更新
     * @param post 待修改的岗位
     */
    @Override
    @Transactional
    public void updatePost(Post post) {
        post.setUpdatedTime(new Date());
        this.updateById(post);
    }

    /**
     * 逻辑删除
     * @param postIds
     * @param post
     */
    @Override
    @Transactional
    public void updatePostDeletedFlg(String postIds, Post post) {
        List<String> list = Arrays.asList(postIds.split(StringPool.COMMA));
        LambdaUpdateWrapper<Post> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Post::getDeletedFlg,FebsConstant.INVALID);
        updateWrapper.in(Post::getPostId,list);
        this.update(updateWrapper);
    }

    /**
     * 查询所有岗位
     *
     * @param post 岗位
     * @return
     * @author dinght
     * @since 2024-08-14
     */
    @Override
    public List<Post> findDeptForm(Post post) {
        LambdaQueryWrapper<Post> deptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deptLambdaQueryWrapper.eq(Post::getDeletedFlg,FebsConstant.VALID);
        if(StringUtils.isNotBlank(post.getTenantId())){
            deptLambdaQueryWrapper.eq(Post::getTenantId,post.getTenantId());
        }
        if(StringUtils.isNotBlank(post.getPostCode())){
            deptLambdaQueryWrapper.eq(Post::getPostCode,post.getPostCode());
        }
        return this.baseMapper.selectList(deptLambdaQueryWrapper);
    }


}
